package com.controller;

import com.model._do.AppointmentDO;
import com.model._do.DeSignDO;
import com.model._do.SignUpDO;
import com.model._do.UserDO;
import com.model.dto.AppointEquipDTO;
import com.model.dto.AppointmentDTO;
import com.model.dto.SignedRelateDTO;
import com.model.dto.baseDTO.ResultDTO;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping()
public class OthersController {

    @Autowired
    private SignUpService signUpService;

    @Autowired
    private ResultService resultService;

    @Autowired
    private UserService userService;

    @Autowired
    private AppointmentService appointmentService;

    @Autowired
    private DeSignUpService deSignUpService;

    @Autowired
    private EquipmentService equipmentService;
    /**
     * 根据设备id获取医生信息
     * @param sign 带equipId
     * @return
     * 1、qppointment 与签约医生最近一次预约
     * 2、user 签约医生信息
     * 3、sign 与当前医生签约信息
     * 4、design 与当前医生解约信息
     */
    @RequestMapping(value = "/user/getDoctorInfoByEquipId", method = RequestMethod.POST)
    public ResultDTO getEquipDoctor(@RequestBody SignUpDO sign){
        /**
         * 能获取进list的表示，未解约成功的
         * 1、正常 agree
         * 2、正在解约
         */
        List<SignUpDO> signList = signUpService.getSignedByEquipId(sign);
        return resultService.createSuccess( "success",signList.stream().map(this::getDoctorRelate).filter(Objects::nonNull).collect(Collectors.toList()));
    }

    /**
     * 医生根据userId获得最近两日与我 非无效预约
     * @param user 被获取userId
     * @return List {appoint, equip}
     */
    @RequestMapping(value = "/equip/getMyAppointDoctor", method = RequestMethod.POST)
    public ResultDTO getMyAppointDoctor(@RequestBody UserDO user){
        List<AppointmentDO> appointList = appointmentService.getAppointByUser(user);
        List<AppointEquipDTO> aeList = appointList.stream().map(ap-> this.tranToAppointEquip(ap, user.getUserId())).collect(Collectors.toList());
        return resultService.createSuccess("success", aeList);
    }

    private AppointEquipDTO tranToAppointEquip(AppointmentDO appoint, String userId){
        AppointEquipDTO ap = new AppointEquipDTO();
        ap.setAppoint(appoint);
        ap.setEquip(equipmentService.getEquipByEquipId(userId.equals(appoint.getDoAppointEr())? appoint.getAppointEd(): appoint.getDoAppointEr()));
        return ap;
    }

    private SignedRelateDTO getDoctorRelate(SignUpDO sign){
        SignedRelateDTO signedRelate = new SignedRelateDTO();
        try{
            UserDO currentUser = userService.getUserByUserId(sign.getUserId());
            signedRelate.setSign(sign);
            signedRelate.setDoctor(currentUser);
            List<AppointmentDO> appointmentList = appointmentService.getAppointByDocAndEqu(sign, currentUser);
            AppointmentDO appointment = null;
            if(appointmentList.size() > 0) {
                appointment = appointmentList.get(0);
            }
            if(appointment != null){
                signedRelate.setAppointment(new AppointmentDTO(appointment));
            }
            DeSignDO deSign = deSignUpService.getDeSignByFromToId(currentUser.getUserId(), sign.getEquipId());
            if(deSign != null){
                signedRelate.setDeSign(deSign);
            }
        } catch (Exception e){
            return null;
        }
        return signedRelate;
    }

}
